package com.cimstech.model;


import java.util.List;


import java.util.ArrayList;

import monfox.stack.iec61850.api.client.DA;
import monfox.stack.iec61850.api.client.DO;
import monfox.stack.iec61850.api.client.DataSet;
import monfox.stack.iec61850.api.client.FC;
import monfox.stack.iec61850.api.client.IEC61850Client;
import monfox.stack.iec61850.api.client.LD;
import monfox.stack.iec61850.api.client.LN;
import monfox.stack.iec61850.api.client.Log;

public class Nodes {

    public static NodeInfo getNodes(IEC61850Client client) throws Exception {
        NodeInfo node = new NodeInfo(client.getIED().getName(), client.getIED().getName(), "IED");
        node.setNodes(getLDNodes(client.getLDs()));
        return node;

    }

    public static ArrayList<NodeInfo> getLDNodes(List<LD> LDs) throws Exception {
        ArrayList<NodeInfo> lds = new ArrayList<NodeInfo>();
        for (LD ld : LDs) {
            NodeInfo ldNode = new NodeInfo(ld.getName(), ld.getName(), "LD");
            ldNode.setNodes(getLNNodes(ld.getLNs(), ld.getName()));
            lds.add(ldNode);
        }
        return lds;
    }

    public static ArrayList<NodeInfo> getLNNodes(List<LN> LNs, String id) throws Exception {
        ArrayList<NodeInfo> lns = new ArrayList<NodeInfo>();
        for (LN ln : LNs) {
            NodeInfo lnNode = new NodeInfo(ln.getName(), id + "/" + ln.getObjectRef(), "LN");
            lnNode.setNodes(getFCNodes(ln.getFCs(), id + "/" + ln.getObjectRef()));
            lnNode.getNodes().addAll(getDataSetNodes(ln.getDataSets(), id + "/" + ln.getObjectRef()));
            lnNode.getNodes().addAll(getLogNodes(ln.getLogs(), id + "/" + ln.getObjectRef()));
            lns.add(lnNode);
        }
        return lns;
    }

    public static ArrayList<NodeInfo> getFCNodes(List<FC> FCs, String id) throws Exception {
        ArrayList<NodeInfo> fcs = new ArrayList<NodeInfo>();
        for (FC fc : FCs) {
            NodeInfo fcNode = new NodeInfo(fc.getName(), id + "$" + fc.getName(), "FC");
            fcNode.setNodes(getDONodes(fc.getDOs(), id + "$" + fc.getName()));
            fcs.add(fcNode);
        }
        return fcs;
    }

    public static ArrayList<NodeInfo> getDONodes(List<DO> DOs, String id) throws Exception {
        ArrayList<NodeInfo> dos = new ArrayList<NodeInfo>();
        for (DO doo : DOs) {
            NodeInfo doNode = new NodeInfo(doo.getName(), id + "$" + doo.getName(), "DO");
            doNode.setNodes(getDANodes(doo.getDAs(), id + "$" + doo.getName()));
            dos.add(doNode);
        }
        return dos;
    }

    public static ArrayList<NodeInfo> getDANodes(List<DA> DAs, String id) throws Exception {
        ArrayList<NodeInfo> das = new ArrayList<NodeInfo>();
        if (DAs != null) {
            for (DA da : DAs) {
                NodeInfo daNode = new NodeInfo(da.getName(), id + "$" + da.getName(), "DA");
                daNode.setNodes(getDANodes(da.getDAs(), id + "$" + da.getName()));
                das.add(daNode);
            }
        }
        return das;
    }

    public static ArrayList<NodeInfo> getDataSetNodes(List<DataSet> dataSets, String id) throws Exception {
        ArrayList<NodeInfo> dss = new ArrayList<NodeInfo>();
        for (DataSet ds : dataSets) {
            NodeInfo dsNode = new NodeInfo(ds.getName(), id + "$" + ds.getName(), "DS");
            dss.add(dsNode);
        }
        return dss;
    }

    public static ArrayList<NodeInfo> getLogNodes(List<Log> logs, String id) throws Exception {
        ArrayList<NodeInfo> Logs = new ArrayList<NodeInfo>();
        for (Log lg : logs) {
            NodeInfo logNode = new NodeInfo(lg.getName(), id + "$" + lg.getName(), "LG");
            Logs.add(logNode);
        }
        return Logs;
    }
}

/*
@XmlRootElement()
public class Nodes extends IECResult {
    private List<Node> nodes;

    @XmlElement(value = "nodes")
    public List<Node> getNodes() {
        return this.nodes;
    }

    public void setNodes(List<Node> nodes) {
        this.nodes = nodes;
    }
}*/
